package com.lastoffer2.offerview;

import java.util.List;
import java.util.Locale;

import com.example.lastoffer2.R;
import com.lastct.dao.Offer;
import com.lastoffer2.JSONConnector;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ListOfferActivity extends FragmentActivity {

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;

	public List<Offer> data;

	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_offer);

		
		 this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);
		 new DownloadTask().execute("Any parameters my download task needs here");

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list_offer, menu);
		return true;
	}

	private class DownloadTask extends AsyncTask<String, Void, Object> {
        protected Object doInBackground(String... args) {
            Log.i("MyApp", "Background thread starting");
            List<Offer> result = JSONConnector.getListOffers();
            // This is where you would do all the work of downloading your data

            return result;
        }

        protected void onPostExecute(Object result) {
            // Pass the result data back to the main activity
        	ListOfferActivity.this.data = (List<Offer>)result;
        	
            if (ListOfferActivity.this.pd != null) {
            	ListOfferActivity.this.pd.dismiss();
            }
         // Create the adapter that will return a fragment for each of the three
    		// primary sections of the app.
    		mSectionsPagerAdapter = new SectionsPagerAdapter(
    				ListOfferActivity.this.getSupportFragmentManager(), ListOfferActivity.this.data);

    		// Set up the ViewPager with the sections adapter.
    		mViewPager = (ViewPager) findViewById(R.id.pager);
    		mViewPager.setAdapter(mSectionsPagerAdapter);
        }
   }    
	

}
